from datetime import datetime
from flask import render_template, request, jsonify, Blueprint,current_app, make_response
from flask_login.utils import login_required, current_user
from app import util
from app.model.cms import Category

from app.util.common import get_short_id, time_now_name, upload, get_download_file_path
from app.util.permission import admin_perm
from app.util import R, request_form_auto_fill
from app.util.model import query_model
from app.util.dict_data import get_dict_data, get_label_by_value
from app.ext import db

from openpyxl import Workbook
from openpyxl.writer.excel import save_virtual_workbook

from app.admin.views.cms import cms_bp
from app.model import Article,Tag
import os

bp = Blueprint('article', __name__)
cms_bp.register_blueprint(bp, url_prefix='/article')
tpl_prefix = 'admin/cms/article/'
model_name = '文章管理'

@bp.route('/',methods=['GET'])
@login_required
@admin_perm(model_name, '查看列表', permission='cms:article:view')
def index():
    """
    列表
    """
    return render_template( tpl_prefix + 'list.html')

@bp.route('/list',methods=['GET','POST'])
@login_required
@admin_perm(model_name, '获取列表数据', permission='cms:article:view')
def list():
    """
    获取列表
    """
    list = query_model(Article, search_query, need_scope=True)
    return jsonify(list)

def search_query(query):
    query = query.filter(Article.deleted == 0)
    value = request.values.get('title','').strip()
    if value != '':
        query = query.filter(Article.title.like('%%%s%%' % value))
    value = request.values.get('publish_time','').strip()
    if value != '':
        query = query.filter(Article.publish_time == value)
    value = request.values.get('author_id','').strip()
    if value != '':
        query = query.filter(Article.author_id == value)
    value = request.values.get('category_id','').strip()
    if value != '':
        # query = query.filter(Article.category_id == value)
        category = Category.query.get(value)
        query = query.filter(Article.category_id.in_(category._children_ids()))
    

    query = query.order_by(Article.sn.desc(),Article.id.desc())
    value = request.values.get('ids','').strip()
    if value != '':
        ids = value.split(',')
        query = query.filter(Article.id.in_(ids))
    return query


@bp.route('/export',methods=['GET','POST'])
@login_required
@admin_perm(model_name, '导出excel', permission='cms:article:export')
def export():
    list = query_model(Article, search_query, is_page=False, need_scope=True)
    
    title = ["备注","机构id","标题","编码","编辑器","md内容","内容","简述","缩略图","状态","访问统计","","发布时间","","","","1=管理员,2=普通账号,3=vip","是否抓取内容","抓取内容的原始url","原作者"]
    state_datas = get_dict_data('cms_article_state')
    wb = Workbook()
    sheet_names = wb.get_sheet_names()
    ws = wb.get_sheet_by_name(sheet_names[0])
    ws.append(title)
    for row in list:
        data = []
        data.append(row.remark)
        data.append(row.org_id)
        data.append(row.title)
        data.append(row.name)
        data.append(row.editor)
        data.append(row.content)
        data.append(row.content_html)
        data.append(row.summary)
        data.append(row.thumbnail)
        data.append(get_label_by_value(state_datas, row.state))
        data.append(row.vc)
        data.append(row.comment_num)
        data.append(row.publish_time)
        data.append(row.author_id)
        data.append(row.category_id)
        data.append(row.h_content)
        data.append(row.h_role)
        data.append(row.is_crawl)
        data.append(row.origin_url)
        data.append(row.origin_author)
        ws.append(data)
    
    abs_filepath, filepath = get_download_file_path("{}.xlsx".format(time_now_name()))
    wb.save(abs_filepath)
    return R.success(msg=filepath)
    

@bp.route('/add',methods=['GET','POST'])
@login_required
@admin_perm(model_name, '添加', permission='cms:article:add')
def add():
    """
    添加
    """
    if request.method == 'POST': # 添加
        o = Article()
        request_form_auto_fill(o)
        if request.files.get('thumbnail'):
            img_url = upload('thumbnail', 'material')
            o.thumbnail = img_url
        if int(o.state) == 1:
            o.publish_time = datetime.now()
        if o.name == '':
            o.name = get_short_id()
        if type(o.publish_time) == str:
            o.publish_time = datetime.strptime(o.publish_time, '%Y-%m-%d %H:%M:%S')
        db.session.add(o)
        db.session.commit()
        if o.name == '':
            o.name = o.id
            db.session.commit()
        return R.success(msg='成功')
    category = None
    category_id = request.args.get('category_id', type=int)
    if category_id:
        category = Category.query.get(category_id)
    return render_template( tpl_prefix + 'add.html', category = category)

@bp.route('/edit',methods=['GET','POST'])
@login_required
@admin_perm(model_name, '修改', permission='cms:article:edit')
def edit():
    """
    编辑修改
    """
    editor = request.args.get('editor', 'markdown')
    if request.method == 'POST': 
        id = request.form.get('id', 0, type=int)
        o = Article.query.get(id)
        if o is None:
            o = Article()
            o.editor = editor
        request_form_auto_fill(o)
        if o.editor == 'markdown':
            o.content_html,o.toc = o.md_convert()

        if type(o.publish_time) == str:
            o.publish_time = datetime.strptime(o.publish_time, '%Y-%m-%d %H:%M:%S')
        name = o.name
        if o.id is None or o.id == '':
            o.id = None
            db.session.add(o)
        if name is None or name == '':
            if o.id is None:
                o.name = get_short_id()
            else:
                o.name = o.id
        db.session.commit()
        if name is None or name == '':
            if o.id:
                o.name = o.id
        db.session.commit()
        # 更新tags
        tags = request.form.get('tagsinput','')
        o.tags = []
        for tag in tags.split(','):
            if tag.strip() == '':
                continue
            t = Tag.add(tag)
            if t not in o.tags:
                o.tags.append(t)
        return R.success()

    id = request.args.get('id', 0, type=int)
    o = Article.query.get(id)
    if o is None:
        o = Article()
        o.vc = 0
        category_id = request.args.get('category_id', type=int)
        if category_id:
            category = Category.query.get(category_id)
            setattr(o, 'category_tmp', category)
            o.category_id = category.id
        else:
            category = Category()
            category.title = ''
            setattr(o, 'category_tmp', category)
            o.category_id = category.id
    if editor == '':
        if o:
            editor = o.editor
        else:
            editor = current_app.config.get('H3BLOG_EDITOR')
    if editor == 'tinymce':
        return render_template(tpl_prefix + 'edit_tinymce.html', d = o)
    return render_template(tpl_prefix + 'edit_md.html', d = o)

@bp.route('/remove',methods=['GET','POST'])
@login_required
@admin_perm(model_name, '删除', permission='cms:article:remove')
def remove():
    """
    根据id删除
    """
    ids = request.values.get('ids', type=str)
    ids = [int(i) for i in ids.split(',')]
    Article.query.filter(Article.id.in_(ids)).update({'deleted': 1})
    return R.success()


@bp.post('/change_state')
@login_required
@admin_perm(model_name, '修改文章状态')
def change_state():
    """
    修改文章状态
    """
    id = request.form.get('id', 0, type=int)
    state = request.form.get('state', '', type=int)
    a = Article.query.get(id)
    if a:
        a.state = state
        if a.state ==1 and a.publish_time is None:
            a.publish_time = datetime.now()
        db.session.commit()
        return R.success(msg='操作成功')
    return R.error(msg='操作失败')


@bp.route('/import_article',methods=['POST'])
def import_article():
    from readability import Document
    url = request.form.get('url')
    download_img = request.form.get('download_img', 0, type=int)
    is_download_img = True if download_img == 1 else False
    req_html = request.form.get('html')
    if req_html and len(req_html) > 0:
        html = req_html
    else:
        html = util.open_url(url)
        html = util.strdecode(html)
        
        doc = Document(html)
        html = doc.summary()
    markdown = util.html2markdown(html, url, is_download_img, '')
    return jsonify(markdown)

@bp.post('/exchange_sn')
@login_required
@admin_perm(model_name, '交换文章排序')
def exchange_sn():
    """
    交换文章排序
    """
    from_id = request.form.get('from_id', type=int)
    to_id = request.form.get('to_id', type=int)
    from_a = Article.query.get(from_id)
    to_a = Article.query.get(to_id)
    from_sn = from_a.sn
    from_a.sn = to_a.sn
    to_a.sn = from_sn
    return R.success(msg='操作成功')

@bp.post('/change_sn')
@login_required
@admin_perm(model_name, '修改文章排序')
def change_sn():
    """
    修改文章排序
    """
    id = request.form.get('id', type=int)
    sn = request.form.get('sn', type=int)
    article = Article.query.get(id)
    article.sn = sn
    return R.success(msg='操作成功')

@bp.post('/change_category')
@login_required
@admin_perm(model_name, '修改所属栏目')
def change_category():
    """
    修改所属栏目
    """
    ids = request.form.get('ids','')
    ids = [int(id.strip()) for id in ids.split(',') if id.strip() != '']
    category_id = request.form.get('category_id', type=int)
    Article.query.filter(Article.id.in_(ids)).update({'category_id': category_id})
    return R.success(msg='操作成功')